#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(dolphin, 1.0-U2, dolphincn@live.cn)
AC_CONFIG_SRCDIR([src/server.c])
AC_CONFIG_HEADER([src/config.h])
AC_PREFIX_DEFAULT(/usr/local/dolphin)
AM_INIT_AUTOMAKE

# Checks for programs.
AC_PROG_CC
AC_PROG_CC_C_O
AC_PROG_LIBTOOL
# Checks for libraries.

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([inttypes.h \
				  limits.h \
				  netinet/in.h \
				  stdint.h \
				  stdlib.h \
				  string.h \
				  unistd.h \
				  sys/mman.h \
				  sys/sendfile.h \
				  stddef.h \
				  lua.h \
				  lualib.h \
				  lauxlib.h \
				  sys/socket.h \
				  strings.h \
				  sys/uio.h \
				  stdio.h \
				  openssl/md5.h \
				  sys/wait.h \
				  sys/resource.h \
				  sys/time.h \
				  sys/types.h \
				  sys/event.h \
				  ctype.h \
				  libxml/parser.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memchr \
				memmove \
				memset \
				strcasecmp \
				strdup \
				strndup \
				sigaction \
				timegm \
				WIFEXITED])


have_lua_lib="no"
AC_MSG_CHECKING(for lua)
AC_ARG_WITH(lua, [ --with-lua      Path of lua install dir], 
			[if test $withval != "no"; then
					if test $withval != "yes"; then
						LUALIBS="-L$withval -llua"
					else 
						LUALIBS="-L/usr/local/lib -llua"
					fi
					AC_SUBST(LUALIBS)
					have_lua_lib="yes"
			fi], 
			[LUALIBS=""
			 AC_SUBST($LUALIBS)
			 have_lua_lib="no"
			 ])
AC_TRY_COMPILE([#include <lua.h>], 
			   [luaL_newstate()], 
			[AC_MSG_RESULT(yes)
			 have_lua_lib="yes"] ,
			[AC_MSG_RESULT(no)
			 have_lua_lib="no"])

dnl==========Do use ini ?===================
use_ini="no"
AC_MSG_CHECKING(for whether to use ini)
AC_ARG_WITH(ini, [ --with-ini      Use ini as configure language], 
			[if test $withval != "no"; then
			 	use_ini="yes"
			fi], 
			[
			 use_ini="no"
			 ])

if test $use_ini = "yes"; then
	AC_MSG_RESULT(use ini as configure language)
	AC_DEFINE_UNQUOTED(USE_INI, 1, [Use ini as confiugure language])
else 
	if test $have_lua_lib = "no"; then
		AC_MSG_RESULT(use ini because not find lua lib)
		AC_DEFINE_UNQUOTED(USE_INI, 1, [Use ini as confiugure language])
	else 
		AC_MSG_RESULT(don't use ini as configure language)
		AC_DEFINE_UNQUOTED(USE_INI, 0, [Don't use ini as confiugure language])
	fi
fi
	
dnl =============================================================
AC_MSG_CHECKING([whether our compiler supports __func__])
AC_TRY_COMPILE([],
 [ const char *cp = __func__; ],
 AC_MSG_RESULT([yes]),
 AC_MSG_RESULT([no])
 AC_MSG_CHECKING([whether our compiler supports __FUNCTION__])
 AC_TRY_COMPILE([],
   [ const char *cp = __FUNCTION__; ],
   AC_MSG_RESULT([yes])
   AC_DEFINE(__func__, __FUNCTION__,
         [Define to appropriate substitue if compiler doesnt have __func__]),
   AC_MSG_RESULT([no])
   AC_DEFINE(__func__, __FILE__,
         [Define to appropriate substitue if compiler doesnt have __func__])))

dnl =============================================================

AC_MSG_CHECKING(for pthread)
LIBS="$LIBS -lpthread"
AC_TRY_LINK([#include <pthread.h>],
			  [pthread_exit(NULL);], 
			[AC_MSG_RESULT(yes)] ,
			[AC_MSG_ERROR(no)])

AC_OUTPUT([Makefile src/Makefile])
